package com.bilibili.controller;

import com.bilibili.dto.FileUploadDto;
import com.bilibili.exception.BusinessException;
import com.bilibili.manage.OssService;
import com.bilibili.utils.RandomUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 上传相关的 controller接口
 *
 * @Author YM
 */
@RestController
@RequestMapping("upload")
public class UploadController {
    @Autowired
    private OssService ossService;

    /**
     * 上传单个文件
     *
     * @param file 需要上传的文件
     * @return 文件路径
     */
    @RequestMapping(value = "one", method = RequestMethod.POST)
    public FileUploadDto uploadOne(@RequestBody MultipartFile file) {
        FileUploadDto fileUploadDto = new FileUploadDto();
        //1.获取文件的名字
        //(1)获取文件的原名
        String originalFileName = file.getOriginalFilename();
        //(2)把文件的名字重新生成，防止保存对的目录文件重名导致覆盖等问题
        String newName = RandomUtils.generateUuid() + "." + FilenameUtils.getExtension(originalFileName);
        //2.调用Service层上传方法
        try (InputStream inputStream = file.getInputStream()) {
            fileUploadDto.setFileName(ossService.uploadFile(newName, inputStream));
        } catch (Exception e) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "上传失败");
        }
        return fileUploadDto;
    }

    /**
     * 批量上传文件
     *
     * @param files 文件数组
     * @return 文件名字封装的FileUploadDto列表
     */

    @RequestMapping( value = "multi", method = RequestMethod.POST)
    @ResponseBody
    public List<FileUploadDto> uploadMulti(@RequestParam("file") MultipartFile[] files) {
        List<FileUploadDto> fileUploadDtos = new ArrayList<>();
        try {
            if (!ArrayUtils.isEmpty(files)) {
                for (MultipartFile file : files) {
                    FileUploadDto fileUploadDto = new FileUploadDto();
                    //1.获取文件的名字
                    //(1)获取文件的原名
                    String originalFileName = file.getOriginalFilename();
                    //(2)把文件的名字重新生成，防止保存对的目录文件重名导致覆盖等问题
                    String newName = RandomUtils.generateUuid() + "." + FilenameUtils.getExtension(originalFileName);
                    //2.调用Service层上传方法
                    InputStream inputStream = file.getInputStream();
                    fileUploadDto.setFileName(ossService.uploadFile(newName, inputStream));
                    fileUploadDtos.add(fileUploadDto);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(HttpStatus.BAD_REQUEST, "批量上传失败");
        }
        return fileUploadDtos;
    }
}
